package com.dgut.Interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class UserLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器preHandle.....................");
        HttpSession session = request.getSession();
        if(session.getAttribute("userId") != null){
            return true;
        } else {
            System.out.println("拦截器到");
          request.getRequestDispatcher("/").forward(request,response);
          return  false;
        }
    }
}
